package InsLoginRegister;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
@WebServlet(name = "ImageServlet",urlPatterns = "/image")
@MultipartConfig(location = "D:\\Final5\\web\\html\\assets\\images")// location改为本地的绝对路径
public class Image extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            if(request.getParameter("submit")!=null) {
                Part part = request.getPart("file");
                String mno  = request.getSession().getAttribute("mno").toString();
                String  image_path = mno+".jpg";
                part.write(image_path);
                request.getSession().setAttribute("image",image_path);
              response.sendRedirect("http://localhost:8084/html/index.jsp");
            }
            else response.sendRedirect("error.jsp");
        } catch (Exception ex) {
            throw new ServletException(ex);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}